<template>
  <el-form ref="formRef" :model="form" label-width="120px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">按试管码标异常</el-link>
      </h2>
    </el-form-item>
    <el-form-item label="试管码：">
      <el-input v-model="form.tubeCode" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
    <el-form-item label="">
      {{ JSON.stringify(form.tubeCodes) }}
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive } from "vue";
import saveAs from "./js/FileSaver";
// do not use same name with ref
const form = reactive({
  // 身份证号
  tubeCode: "",
  // 审核人数组
  tubeCodes: [],
  // 脚本
  scriptString: ""
});

const add = () => {
  form.tubeCode.trim() ? form.tubeCodes.push(form.tubeCode.trim()) : "";
  form.tubeCode = "";
};

const reset = () => {
  form.tubeCode = "";
  form.tubeCodes = [];
  form.scriptString = "";
};

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(str, `按试管码标异常.txt`);
};

const onSubmit = () => {
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin

先备份：（请将备份的数据写在jira的备注里，如果数据多，可以生成txt文件上传到jira里）
var c = db.nat_tube.find({"tube_code":{"$in":${JSON.stringify(form.tubeCodes)}}})
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)

试管标记异常：
db.nat_tube.find({$and:[{"tube_code":{"$in":${JSON.stringify(form.tubeCodes)}}}]}).forEach(function (tubeObj) {
tubeObj.status = NumberInt(5);
tubeObj.abnormal_sign  = NumberInt(1);
tubeObj.update_abnormal_by = 'admin_manual_abnormal';
db.getCollection('nat_tube').save(tubeObj);
var appoCheckList = db.getCollection('nat_appo_check').find({"tube_id": tubeObj.id}).
forEach( function ( appoCheckObj) {
db.getCollection('nat_appo_check').update({"tube_id":tubeObj.id,id:appoCheckObj.id},{$set:{
status : NumberInt(5),
abnormal_sign:NumberInt(1),
update_abnormal_by:'admin_manual_abnormal'
}},false,true);
print("del appointment."+appoCheckObj.appo_person);
print("del appointment."+appoCheckObj.appo_unique_code);
var relationObj = db.getCollection('nat_appo_check_relation').findOne({"appo_check_id": appoCheckObj.id});
if(relationObj != null){
relationObj.appo_status = NumberInt(5);
relationObj.abnormal_sign = NumberInt(1);
relationObj.update_abnormal_by = 'admin_manual_abnormal';
db.getCollection('nat_appo_check_relation').save(relationObj);
}
});
});`;
};
</script>

<style>
.input-width {
  width: 500px;
}
</style>
